package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsGoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsGoodsCommentServiceImpl implements DtsGoodsCommentService {

    @Autowired
    DtsCommentMapper commentMapper;

    @Override
    public IPage<DtsComment> queryPage(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order, List<Integer> brandIds) {
        IPage<DtsComment> ipage = new Page<>(page, limit);

        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();

        if(userId != null){
            wrapper.eq("user_id", userId);
        }
        if(goodsId != null){
            wrapper.eq("value_id", goodsId);
        }

        if(brandIds != null && brandIds.size() > 0){
            wrapper.in("g.brand_id", brandIds);
        }

        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                wrapper.orderByDesc(sort);
            }else{
                wrapper.orderByAsc(sort);
            }

        }
        ipage = commentMapper.selectGoodsCommentPage(ipage, wrapper);

        return ipage;
    }

    @Override
    public void delete(Integer id) {
        commentMapper.deleteById(id);
    }
}
